package com.soft.javase.ch10.test.stream;

import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wangzhisen
 * @Desc TODO
 * @date 2025/5/7
 *
 * 1.练习Stream中的常用方法of()、iterate()、generate()、findFirst()、findAny()、anyMatch()、distinct()等
 */
public class StreamMethodsPractice {
    public static void main(String[] args) {
        // of() 方法
        Stream<String> stream = Stream.of("A", "B", "C");
        System.out.println("of(): " + stream.collect(Collectors.toList()));

        // iterate() 方法
        Stream<Integer> iterateStream = Stream.iterate(0, n -> n + 2).limit(5);
        System.out.println("iterate(): " + iterateStream.collect(Collectors.toList()));

        // generate() 方法
        Stream<Double> generateStream = Stream.generate(Math::random).limit(3);
        System.out.println("generate(): " + generateStream.collect(Collectors.toList()));

        // findFirst() 方法
        Optional<String> first = Stream.of("Java", "Python", "C++").findFirst();
        System.out.println("findFirst(): " + first.orElse("None"));

        // findAny() 方法
        Optional<String> any = Stream.of("Apple", "Banana", "Orange").parallel().findAny();
        System.out.println("findAny(): " + any.orElse("None"));

        // anyMatch() 方法
        boolean hasNegative = Stream.of(1, 2, -3, 4).anyMatch(n -> n < 0);
        System.out.println("anyMatch(): " + hasNegative);

        // distinct() 方法
        List<Integer> distinctNumbers = Stream.of(1, 2, 2, 3, 3, 3).distinct().collect(Collectors.toList());
        System.out.println("distinct(): " + distinctNumbers);
    }
}
